package y24.m4;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import java.util.Calendar;

public class Test16 {
    static class CalendarBean {
        private String[] week = {"日", "一", "二", "三", "四", "五", "六"};
        private String[] day;
        public int NOW_YEAR = 0;
        public int NOW_MONTH = 0;
        private int year;
        private int month;

        public CalendarBean() {
            Calendar calendar = Calendar.getInstance();
            NOW_YEAR = calendar.get(Calendar.YEAR);
            NOW_MONTH = calendar.get(Calendar.MONTH) + 1;
            year = NOW_YEAR;
            month = NOW_MONTH;
            getDay();
        }

        public void getDay() {
            this.day = new String[42];
            Calendar date = Calendar.getInstance();
            date.set(this.year, this.month-1, 1);
            int week = date.get(Calendar.DAY_OF_WEEK) - 1;
            int daysInMonth;

            switch (this.month) {
                case 4:
                case 6:
                case 9:
                case 11:
                    daysInMonth = 30;
                    break;
                case 2:
                    if (((this.year % 4 == 0 && this.year % 100 != 0) || this.year % 400 == 0)) {
                        daysInMonth = 29;
                    } else {
                        daysInMonth = 28;
                    }
                    break;
                default:
                    daysInMonth = 31;
                    break;
            }

            for (int i = week, n = 1; i < week + daysInMonth; i++) {
                this.day[i] = String.valueOf(n);
                n++;
            }
        }
    }

    static class Frame extends JFrame {
        private CalendarBean calendarBean = new CalendarBean();
        private JButton next;
        private JButton now;
        private JButton previous;
        private JLabel monthLabel;
        private JPanel calendarPanel;

        public Frame() {
            this.monthLabel = new JLabel(calendarBean.year + "年" + calendarBean.month + "月", JLabel.CENTER);
            this.next = new JButton("下一个月");
            this.now = new JButton("当前日期");
            this.previous = new JButton("上一个月");

            next.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (calendarBean.month == 12) {
                        calendarBean.month = 1;
                        calendarBean.year++;
                    } else {
                        calendarBean.month++;
                    }
                    calendarBean.getDay();
                    updateCalendar();
                }
            });

            previous.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (calendarBean.month == 1) {
                        calendarBean.month = 12;
                        calendarBean.year--;
                    } else {
                        calendarBean.month--;
                    }
                    calendarBean.getDay();
                    updateCalendar();
                }
            });
            now.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    calendarBean.month = calendarBean.NOW_MONTH;
                    calendarBean.year = calendarBean.NOW_YEAR;
                    calendarBean.getDay();
                    updateCalendar();
                }
            });

            calendarPanel = new JPanel(new GridLayout(7, 7));
            updateCalendar();


            JPanel topPanel = new JPanel(new BorderLayout());
            topPanel.add(monthLabel, BorderLayout.CENTER);
            Panel p2 = new Panel();
            p2.add(previous);
            p2.add(now);
            p2.add(next);
            topPanel.add(p2, BorderLayout.SOUTH);

            setLayout(new BorderLayout());
            add(topPanel, BorderLayout.NORTH);
            add(calendarPanel, BorderLayout.CENTER);

            setLocationRelativeTo(null); // 窗体居中显示

            setSize(600, 400);
            setBackground(Color.CYAN);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setTitle("日历");
            setVisible(true);

        }

        public void updateCalendar() {
            calendarPanel.removeAll();
            for (String week : calendarBean.week) {
                JLabel label = new JLabel(week);
                label.setHorizontalAlignment(JLabel.CENTER);
                calendarPanel.add(label);
            }

            for (String d : calendarBean.day) {
                JLabel label = new JLabel(d);
                label.setHorizontalAlignment(JLabel.CENTER);
                if (d != null && !d.isEmpty()) {
                    int day = Integer.parseInt(d);
                    if (day == Calendar.getInstance().get(Calendar.DAY_OF_MONTH) && calendarBean.month == calendarBean.NOW_MONTH && calendarBean.year == calendarBean.NOW_YEAR) {
                        label.setForeground(Color.RED);
                        label.setBackground(Color.CYAN);
                    }
                }
                calendarPanel.add(label);
            }

            monthLabel.setText(calendarBean.year + "年" + calendarBean.month + "月");

            revalidate();
            repaint();
        }
    }

    public static void main(String[] args) {
        CalendarBean calendarBean = new CalendarBean();
        calendarBean.getDay();
        System.out.println(Arrays.toString(calendarBean.day));
        new Frame();
    }
}